package controllers

import "LoginTemplate/models"

type DepartmentController struct {
	BaseController
}

type Department struct {
	Id             int    `json:"id"`
	DepartmentName string `json:"department_name"`
}

// 添加部门
func (u *DepartmentController) InsertDepartment() {
	var department Department
	err := u.ShouldBind(&department)
	if err != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	if department.DepartmentName == "" {
		u.JsonMsg(500, "", "请输入部门名称")
	}
	sql := "insert into department ( department_name ) values ( ? )"
	tx := models.DB.Debug().Exec(sql, department.DepartmentName)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

// 修改部门
func (u *DepartmentController) UpdateDepartment() {
	var department Department
	err := u.ShouldBind(&department)
	if err != nil {
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	if department.DepartmentName == "" {
		u.JsonMsg(500, "", "请输入部门名称")
	}
	sql := "update department set department_name = ? where id = ?"
	tx := models.DB.Debug().Exec(sql, department.DepartmentName, department.Id)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

// 删除部门
func (u *DepartmentController) DeleteDepartment() {
	var department Department
	err := u.ShouldBind(&department)
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	sql := "delete from department where id = ?"
	tx := models.DB.Debug().Exec(sql, department.Id)
	if tx.Error != nil {
		u.JsonMsg(500, "", "失败")
	}
	u.JsonMsg(200, "", "成功")
}

type PictureData struct {
	Code        int          `json:"code"`
	Msg         string       `json:"msg"`
	Data        []Department `json:"data"`
	Sum         int          `json:"sum"`         //总页数
	CurrentPage int          `json:"currentPage"` //当前页
	TotalNumber int          `json:"totalNumber"` //总条数
}
type PictureList struct {
	Page int `json:"page"`
	Size int `json:"size"`
	Id   int `json:"id"`
}

// 查询部门
func (u *DepartmentController) SelectDepartment() {
	var pictureList PictureList
	err := u.ShouldBind(&pictureList)
	var sum int
	var totalNumber int
	if err != nil {
		println(err.Error())
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var departemnt []Department
	sql := "select * from department limit ? , ?"
	tx := models.DB.Debug().Raw(sql, (pictureList.Page-1)*pictureList.Size, pictureList.Size).Scan(&departemnt)
	sqlsum := "SELECT CEIL(COUNT(*) / ? ) FROM department"
	models.DB.Debug().Raw(sqlsum, pictureList.Size).Scan(&sum)
	sqlNumBer := "select count(*) from department"
	models.DB.Debug().Raw(sqlNumBer).Scan(&totalNumber)
	if tx.Error != nil {
		//picturedata.Code = 500
		//picturedata.Msg = "查询错误"
		//return picturedata
		u.JsonMsg(500, "", "请求异常，请稍候再试")
	}
	var picturedata PictureData
	picturedata.Code = 200
	picturedata.Data = departemnt
	picturedata.Msg = "成功"
	picturedata.Sum = sum
	picturedata.CurrentPage = pictureList.Page
	picturedata.TotalNumber = totalNumber
	//return picturedata
	u.JsonMsg(200, map[string]interface{}{
		"list":  picturedata,
		"count": "",
	}, "success")
}
